<?php
namespace Hive;
/**
 * Company Object. 
 * 
 * It represant a Company.
 *
 * @author Xavier VILAIN
 * @package hr
 * @property int 	$expenseid		Expense Unique DB Id
 * @property int 	$payment_mode	Payment Mode
 * @property string $type			Type of expense
 * 
 * @property bool 	$loekz 			Deletion Flag if true is an been deleted
 */
use Fox\Dao\SqlFragment;

use Fox\Document;

class Expense extends \Fox\Object\Versioned {

	/**
	 * Expense Message Class
	 * @var string
	 */
	const MSGCL = "expenses";
	
	/**
	 * Expense has been send
	 * @var string
	 */
	const STATUS_NEW = "0";	
	
	/**
	 * Expense has been send
	 * @var string
	 */
	const STATUS_SENT = "1";	
	
	/**
	 * Expense has been validated
	 * @var string
	 */
	const STATUS_VALIDATED = "2";
	
	/**
	 * Expense has been refused
	 * @var string
	 */
	const STATUS_REFUSED = "9";
	
	/**
	 * Document type 
	 * @var string
	 */
	const DOC_TYPE = "expense_file";
	
	/**
	 * 
	 * List of attached Files
	 * @var array
	 */
	private $documents = null;
	
	/**
	 * 
	 * Attach a document to the expense
	 */
	public function attach(\Fox\Document $document)
	{

		// Attach Document Object
		if($document->exists())
		{
			
		}
		else
		{
			$data['language'] = \Xview::instance()->language();
			$data['doctype'] = Expense::DOC_TYPE;
			$document->setFields($data);
			$this->documents[] = $document;
		}
	}
	
	/**
	 * (non-PHPdoc)
	 * @see Fox\Object.Versioned::beforeVersionCreate()
	 */
	protected function beforeVersionCreate()
	{
		// Set Default User 
		$this->idusr = \User::current()->getId();
		$this->status = self::STATUS_NEW;
		
		if(is_array($this->documents))
		{
			foreach($this->documents as $document)
			{
				if(!$document->exists())
				{
					$document->create();
				}
				// @TODO change here to setup multiple document mode
				$this->documentid = $document->getId();
			}
		}
		
	}
	
	/**
	 * (non-PHPdoc)
	 * @see Fox\Object.Versioned::beforeVersionUpdate()
	 */
	protected function beforeVersionUpdate($changed)
	{
		if(is_array($this->documents))
		{
			foreach($this->documents as $document)
			{
				if(!$document->exists())
				{
					$document->create();
					$changed = true;
				}
				elseif($document->updated())
				{
					$changed = true;
					// @TODO change here to setup multiple document mode
					$this->documentid = $document->getId();
				}
			}
		}
		return $changed;
	}
	
	/**
	 * 
	 * Load associated documents
	 * @return array
	 */
	public function documents()
	{
		if($this->exists() && empty($this->documents))
		{
			// Mode 1 (only one document per expense )
			$finder = new \Fox\Document();
			$this->documents = $finder->find(new SqlFragment("id = ?",array($this->documentid)));
			// Mode 2 (multiple documents per expense)
			// @TODO
		}
		return $this->documents;
	}
	
	/**
	 * 
	 * Find and Return expense object reference
	 * @return mixed 
	 */
	public function reference()
	{
		
	}
}
\Fox\Dao::infos(
	Expense::className(),
	new \Fox\Object\Info('expenses', \Fox\Databases::DB_APP)
)->addAutoIncrementField('expenseid');